package cn.edu.ujn.finaltest.controller;

import java.sql.Timestamp;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.deser.Deserializers.Base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.finaltest.dao.BaseDict;
import cn.edu.ujn.finaltest.dao.Customer;
import cn.edu.ujn.finaltest.dao.User;
import cn.edu.ujn.finaltest.service.IBaseDictService;
import cn.edu.ujn.finaltest.service.ICustomerService;
import cn.edu.ujn.finaltest.util.Page;

@Controller
public class BaseDictController {
	// 依赖注入
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private IBaseDictService baseDictService;
	
	/**
	 * 根据dictItemName或dictTypeName，查询匹配信息的数据字典
	 * @param page 页数
	 * @param rows 每页中的行数
	 * @param custName
	 * @param custSource
	 * @param custIndustry
	 * @param custLevel
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/basedict/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows, 
			String dictItemName, String dictTypeName, Model model) {

		// 获取所有数据字典类型
		List<BaseDict> selectAll = baseDictService.selectAll();
		// baseDictTypeList保存所有数据字典类型
		List<BaseDict> dictTypeList = new ArrayList<BaseDict>();
		// 遍历后台数据库中的所有数据字典列表
		for (BaseDict allBaseDict : selectAll) {
			// 遍历保存数据字典某一类型(如客户来源、客户级别等)的数据字典列表
			Boolean exist = false;
			for (BaseDict baseDcitType : dictTypeList) {
				// 如果数据字典类型已经被保存，结束循环
				if (baseDcitType.getDict_type_code().equals(allBaseDict.getDict_type_code())) {
					exist = true;
					break;
				}
			}
			// exist等于false，说明该类型的数据字典还没有被保存,执行添加
			if (exist == false) {
				dictTypeList.add(allBaseDict);
			}
		}
		// 查询数据
		Page<BaseDict> baseDicts = this.baseDictService.findBaseDictList(page, rows, dictItemName, dictTypeName);
		// 是否启用
		HashMap<String, String> isEable = new HashMap<String, String>();
		isEable.put("1", "是");
		isEable.put("0", "否");
		// 添加参数
		model.addAttribute("dictTypeList", dictTypeList);		// 查询参数
		model.addAttribute("page", baseDicts);					// 查询结果
		model.addAttribute("isEnable", isEable);				// 是否启动
		
		return "basedict";
	}
	
	/**
	 * 创建数据字典
	 */
	@RequestMapping("/basedict/create.action")
	@ResponseBody
	public String baseDictCreate(String dictItemName, String dictTypeNameAndCode, String dictEnable) {
	    BaseDict baseDict = new BaseDict();
		String[] split = dictTypeNameAndCode.split("&");
		String dictTypeName = split[0];
		String dictTypeCode = split[1];
		baseDict.setDict_item_name(dictItemName);
		baseDict.setDict_type_name(dictTypeName);
		baseDict.setDict_type_code(dictTypeCode);
		baseDict.setDict_enable(dictEnable);
		
		// 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = this.baseDictService.insertSelective(baseDict);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	
	/**
	 * 通过id获取数据字典信息
	 */
	@RequestMapping("/basedict/getBaseDictById.action")
	@ResponseBody
	public BaseDict getBaseDictById(Integer id) {
		BaseDict baseDict = this.baseDictService.selectByPrimaryKey(id.toString());
	    return baseDict;
	}
	
	/**
	 * 更新字典项
	 */
	@RequestMapping("/basedict/update.action")
	@ResponseBody
	public String baseDictUpdate(BaseDict updateBaseDict) {
		// 根据修改后的typeName，修改为对应TypeCode
		System.out.println(updateBaseDict);
 		BaseDict baseDictForTypeCode = new BaseDict();
		baseDictForTypeCode.setDict_type_name(updateBaseDict.getDict_type_name());
		List<BaseDict> selectBaseDictList = this.baseDictService.selectBaseDictList(baseDictForTypeCode);
		String newDictTypeCode = selectBaseDictList.get(0).getDict_type_code();
		updateBaseDict.setDict_type_code(newDictTypeCode);
		int rows = this.baseDictService.updateByPrimaryKey(updateBaseDict);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
}
